<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.starId ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="星级" prop="starClass">
      <span>
          <el-rate
            v-model="dataForm.starClass"
            :colors="colors">
          </el-rate>
      </span>
    </el-form-item>
    <el-form-item label="内容" prop="commentContent">
      <el-input type="textarea" v-model="dataForm.commentContent" placeholder="内容" style="heigth:200px"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
        dataForm: {
          starId: 0,
          commentContent: '',
          starClass: '',
          createTime: '',
          createUserId: '',
          state: ''
        },
        dataRule: {
          commentContent: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          starClass: [
            { required: true, message: '星级不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.starId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.starId) {
            this.$http({
              url: this.$http.adornUrl(`/comment/star/getCommentStarDetailsById/${this.dataForm.starId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commentContent = data.data.commentContent
                this.dataForm.starClass = data.data.starClass
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/comment/star/${!this.dataForm.starId ? 'addCommentStar' : 'modifyCommentStarById'}`),
              method: 'post',
              data: this.$http.adornData({
                'starId': this.dataForm.starId || undefined,
                'commentContent': this.dataForm.commentContent,
                'starClass': this.dataForm.starClass
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
